/***************************************
* Jerato Ver 0.1
* Copyright 2008 Beta.Ito
* MIT License
***************************************/
package com.googlecode.jerato.library;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.googlecode.jerato.core.Service;
import com.googlecode.jerato.library.store.StoreService;
import com.googlecode.jerato.library.store.StoreTransactionService;
import com.googlecode.jerato.library.view.ViewService;

public class SystemService implements Service{
	protected static final Logger LOG = Logger.getLogger(SettingsParser.class.getName());
	protected StoreTransactionService _transaction;
	protected FunctionService _function;
	protected StoreService _dataService;
	protected static SystemService _system;
	protected Map _serviceMap = new HashMap();

	protected SystemService(){
		_system = this;
	}

	public static SystemService getInstance(){
		if(_system == null){
			throw new SystemException("System service not initialized.");
		}
		return _system;
	}

	public static void staticInitialize(){
		if(_system != null){
			return;
		}
		_system = new SystemService();
		_system.startup();
	}
	public static void staticInitialize(SystemService service){
		if(service == null){
			return;
		}
		_system = service;
		_system.startup();
	}

	public static void staticFinalize(){
		if(_system == null){
			return;
		}
		_system.shutdown();
		_system = null;
	}

	public StoreTransactionService getTransactionService(){
		return _transaction;
	}

	public void startup(){
		_transaction = new StoreTransactionService();
		_function = new FunctionService();
		_dataService = new StoreService();
		_dataService.startup();
		_transaction.startup();
		ViewService viewService = new ViewService();
		viewService.startup();
		_serviceMap.put("transactionService", _transaction);
		_serviceMap.put("dataService", _dataService);
		_serviceMap.put("viewService", viewService);
	}

	public void shutdown(){
		if(_transaction != null){
			_transaction.shutdown();
		}
		_transaction = null;
	}
	
	public Service getService(String serviceName){
		if(serviceName == null){
			throw new IllegalArgumentException("serviceName is null");
		}
		return (Service)_serviceMap.get(serviceName);
	}

	public static void main(String[] args) {
//		if(true){
//			throw new RuntimeException();
//		}
		try{
			SystemService.staticInitialize();
//			SystemService system = SystemService.getInstance();
			LOG.info("startup");
//			DataSelect select = new DataSelect();
//			List list = select.executeSelect(system._transaction.createTransaction(), "SELECT SYSDATE FROM DUAL", null);
//			LOG.info(list.toString());
			HashMap map = new HashMap();
			map.put("key", null);
		}
		finally{
			LOG.info("shutdown");
			SystemService.staticFinalize();
		}
	}

	public FunctionService getFunction() {
		return _function;
	}

	public void setFunction(FunctionService function) {
		this._function = function;
	}
	
	public InputStream getResourceAsStream(String resourcePath){
		return ClassLoader.getSystemResourceAsStream(resourcePath);
	}
}
